﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

using YLClient.Util;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace YLClient.Froms
{
    /// <summary>
    /// Interaction logic for NewItem.xaml
    /// </summary>
    public partial class NewItem : Window
    {
        //drag enter的时候拷贝btnDataContainer.Content，
        //如果发生drag leave 那么用此值赋予btnDataContainer.Content
        private List<string> tempFiles;
        private List<string> currentFiles;
        /// <summary>
        /// 上传新的drag项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effects = DragDropEffects.Link;
                String[] pathes = e.Data.GetData(DataFormats.FileDrop) as string[];
                var files = getFilesFromDragDrop(pathes);
                var temp = files.Union(currentFiles).ToList();
                temp.Sort();
                currentFiles = temp;
                currentFilesChange();
            }
        }
        private void Window_DragLeave(object sender, DragEventArgs e)
        {
            currentFiles = tempFiles;
            currentFilesChange();
        }
        private void currentFilesChange()
        {
            lstFiles.UpdateLayout();
        }

        /// 获取拖拽上来的所有文件全路径名
        /// </summary>
        /// <param name="pathes"></param>
        /// <returns></returns>
        private List<string> getFilesFromDragDrop(string[] pathes)
        {
            var ret = pathes.SelectMany(
                path =>
                {
                    if (path.Contains('.'))
                        return singleToIEnumerable(path);
                    else
                    {
                        return Common.getDirFiles(path);
                    }
                }).ToList();
            return ret;
        }
    
    }
}
